package com.handinfo.android.core.graphics;

import java.util.HashMap;

import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;

import com.handinfo.android.DWGameManager;

public class DWFont
{
	/** 默认字体 */
//	public static final String DEFAULT_FONT_NAME = "SansSerif";
	public static final String DEFAULT_FONT_NAME = "宋体";

	/** 字体样式 */
	public static final int STYLE_PLAIN = 0;
	public static final int STYLE_BOLD = 1;
	public static final int STYLE_ITALIC = 2;
	public static final int STYLE_UNDERLINED = 4;

	/** 标准字符 */
	private static final String STANDARD = "X";

	/** 字体集合 */
	private static HashMap<String, DWFont> Fonts = new HashMap<String, DWFont>(20);

	/** Typeface */
	private Typeface m_typeface;

	/** 字体大小 */
	private int m_fontSize;

	/** 字体画笔 */
	private TextPaint m_paint;

	/**
	 * 
	 * @param paint
	 */
	private DWFont(TextPaint paint)
	{
		this.m_paint = paint;
	}

	/**
	 * 
	 * @param typeface
	 * @param fontSize
	 */
	private DWFont(Typeface typeface, int fontSize)
	{
		this(createPaint(typeface), fontSize);
	}

	/**
	 * 
	 * @param typeface
	 * @param path
	 * @param fontSize
	 */
	private DWFont(Typeface typeface, String path, int fontSize)
	{
		this(createPaint(typeface), path, fontSize);
	}

	/**
	 * 
	 * @param typefacePaint
	 * @param fontSize
	 */
	private DWFont(TextPaint typefacePaint, int fontSize)
	{
		m_fontSize = fontSize;
		m_paint = typefacePaint;
	}

	/**
	 * 
	 * @param paint
	 * @param path
	 * @param fontSize
	 */
	private DWFont(TextPaint paint, String path, int fontSize)
	{
		Typeface face = Typeface.createFromAsset(DWGameManager.getInstance().getCurrentActivity().getAssets(), path);
		m_fontSize = fontSize;
		m_paint = paint;
		m_paint.setTypeface(face);
		m_paint.setTextSize(fontSize);
	}

	/**
	 * 根据typeface创建paint
	 * @param typeface
	 * @return
	 */
	private static TextPaint createPaint(Typeface typeface)
	{
		TextPaint paint = new TextPaint();
		paint.setTypeface(typeface);
		return paint;
	}

	public TextPaint getPaint()
	{
		return m_paint;
	}

	public Typeface getTypeface()
	{
		return m_typeface;
	}

	/**
	 * 获得字体大小
	 * @return
	 */
	public int getFontSize()
	{
		return m_fontSize;
	}

	/**
	 * 获得默认字体
	 * @return
	 */

	public static DWFont getDefaultFont()
	{
		int size = 20;
		switch (DWGameManager.AdpaterType) {
			case DWGameManager.AD_HIGH:
				size = 28;
				break;
			default:
				size = 20;
				break;
		}
		return DWFont.getFont(DEFAULT_FONT_NAME, 0, size);
	}

	/**
	 * 根据size得到字体
	 * @param size
	 * @return
	 */
	public static DWFont getFont(int size)
	{
		return DWFont.getFont(DEFAULT_FONT_NAME, 0, size);
	}

	/**
	 * 根据以下参数获得字体
	 * @param familyName
	 * @param style
	 * @param size
	 * @return
	 */
	public static DWFont getFont(String familyName, int style, int size)
	{
		String name = (familyName + style + size).toLowerCase();
		DWFont font = Fonts.get(name);
		if (font == null) {
			if (familyName != null) {
				if (familyName.equalsIgnoreCase("Serif")
						|| familyName.equalsIgnoreCase("TimesRoman")) {
					familyName = "serif";
				}
				else if (familyName.equalsIgnoreCase("SansSerif")
						|| familyName.equalsIgnoreCase("Helvetica")) {
					familyName = "sans-serif";
				}
				else if (familyName.equalsIgnoreCase("Monospaced")
						|| familyName.equalsIgnoreCase("Courier")
						|| familyName.equalsIgnoreCase("Dialog")) {
					familyName = "monospace";
				}
			}
			Typeface face = Typeface.create(familyName, style);
			TextPaint paint = new TextPaint();
			paint.setTypeface(face);
			paint.setTextSize(size);
			paint.setAntiAlias(true);
			Fonts.put(name, font = new DWFont(paint, size));
		}
		return font;
	}

	public boolean isPlain()
	{
		return m_typeface.getStyle() == STYLE_PLAIN;
	}

	public boolean isBold()
	{
		return m_typeface.isBold();
	}

	public boolean isItalic()
	{
		return m_typeface.isItalic();
	}

	public boolean isUnderlined()
	{
		return m_paint.isUnderlineText();
	}

	public FontMetrics getFontMetrics()
	{
		return m_paint.getFontMetrics();
	}

	public float charWidth(char ch)
	{
		char[] chars = Character.toChars(ch);
		float w = m_paint.measureText(chars, 0, 1);
		return w;
	}

	public float stringWidth(String str)
	{
		return m_paint.measureText(str);
	}

	public float getFontHeight()
	{
		return m_paint.getFontMetrics().descent - m_paint.getFontMetrics().ascent;
	}

	public int getHeight()
	{
		return m_paint.getFontMetricsInt(m_paint
				.getFontMetricsInt());
	}
}
